package com.example.parkdemo.visitor

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.DatePicker
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.base.net.Const
import com.example.parkdemo.R
import com.example.parkdemo.databinding.ActivityAddVisitorBinding
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.text.SimpleDateFormat
import java.util.Calendar

@AndroidEntryPoint
class AddVisitorActivity : BaseActivity<ActivityAddVisitorBinding, VisitorViewModel>() {
    private var startTime = 0
    private var endTime = 0
    override fun initData() {
        vm.addVisitorSuccess.observe(this) {
            ToastUtils.showLong("提交成功")
            finish()
        }
    }

    override fun initView() {
        binding.addVisitorHeader.headerTitle.text = "添加来访"
        binding.addVisitorHeader.headerBack.setOnClickListener { finish() }

        binding.parkingStartTimeIv.setOnClickListener {
            val calendar = Calendar.getInstance()
            DatePickerDialog(
                this,
                object : OnDateSetListener {
                    override fun onDateSet(
                        view: DatePicker?,
                        year: Int,
                        month: Int,
                        dayOfMonth: Int
                    ) {
                        binding.parkingStartTimeNowTv.text = "$year-$month-$dayOfMonth "
                        startTime = dayOfMonth
                    }

                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONDAY),
                calendar.get(Calendar.DAY_OF_MONTH)
            ).show()

        }
        binding.parkingEndTimeIv.setOnClickListener {
            val calendar = Calendar.getInstance()
            DatePickerDialog(
                this,
                object : OnDateSetListener {
                    override fun onDateSet(
                        view: DatePicker?,
                        year: Int,
                        month: Int,
                        dayOfMonth: Int
                    ) {
                        binding.parkingEndTimeNowTv.text = "$year-$month-$dayOfMonth "
                        endTime = dayOfMonth
                        val tampTime = endTime - startTime
                        if (tampTime < 0) {
                            ToastUtils.showLong("来访时间小于0天,请重新选择")
                        } else {
                            binding.parkingTimeNowTv.text = "$tampTime 天"
                        }
                    }

                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONDAY),
                calendar.get(Calendar.DAY_OF_MONTH)
            ).show()

        }
        binding.parkingCommentBtn.setOnClickListener {
            val startTime = binding.parkingStartTimeNowTv.text.toString()
            val endTime = binding.parkingEndTimeNowTv.text.toString()
            val time = binding.parkingTimeNowTv.text.toString()
            val desc = binding.visitorText.text.toString()
            val carNum = binding.parkingCarNameNowTv.text.toString()
            if (startTime.isNotEmpty() && endTime.isNotEmpty() && time.isNotEmpty() && desc.isNotEmpty() && carNum.isNotEmpty()) {
                val map = mutableMapOf<String, String>()
                map[Const.PARAM_VISITOR_START] = startTime
                map[Const.PARAM_VISITOR_END] = endTime
                map[Const.PARAM_VISITOR_TIME] = time
                map[Const.PARAM_VISITOR_DESCRIBE] = desc
                map[Const.PARAM_CAR_NUM] = carNum
                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                vm.getAddVisitor(body)

            }

        }
    }

}